package com.ruoyi.web.controller.nongjt;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.nongjt.domain.CertificateIssuanceStats;
import com.ruoyi.nongjt.service.CertificateIssuanceStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 合格证开具数量统计Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/nongjt/certificateIssuanceStats")
public class CertificateIssuanceStatsController extends BaseController
{
    @Autowired
    private CertificateIssuanceStatsService certificateIssuanceStatsService;

    /**
     * 查询合格证开具数量统计列表
     */
    @PreAuthorize("@ss.hasPermi('nongjt:certificateIssuanceStats:list')")
    @GetMapping("/list")
    public TableDataInfo list(CertificateIssuanceStats certificateIssuanceStats)
    {
        startPage();
        List<CertificateIssuanceStats> list = certificateIssuanceStatsService.selectCertificateIssuanceStatsList(certificateIssuanceStats);
        return getDataTable(list);
    }

    /**
     * 导出合格证开具数量统计列表
     */
    @PreAuthorize("@ss.hasPermi('nongjt:certificateIssuanceStats:export')")
    @Log(title = "合格证开具数量统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CertificateIssuanceStats certificateIssuanceStats)
    {
        List<CertificateIssuanceStats> list = certificateIssuanceStatsService.selectCertificateIssuanceStatsList(certificateIssuanceStats);
        ExcelUtil<CertificateIssuanceStats> util = new ExcelUtil<CertificateIssuanceStats>(CertificateIssuanceStats.class);
        util.exportExcel(response, list, "合格证开具数量统计数据");
    }

    /**
     * 生成统计数据
     */
    @PreAuthorize("@ss.hasPermi('nongjt:certificateIssuanceStats:generate')")
    @Log(title = "生成统计数据", businessType = BusinessType.INSERT)
    @PostMapping("/generate/{statsDate}")
    public AjaxResult generateStats(@PathVariable("statsDate") String statsDate)
    {
        try {
            int result = certificateIssuanceStatsService.generateStatsData(statsDate);
            return success("生成成功，共生成 " + result + " 条统计数据");
        } catch (Exception e) {
            return error("生成失败：" + e.getMessage());
        }
    }

    /**
     * 批量生成统计数据
     */
    @PreAuthorize("@ss.hasPermi('nongjt:certificateIssuanceStats:generate')")
    @Log(title = "批量生成统计数据", businessType = BusinessType.INSERT)
    @PostMapping("/batchGenerate")
    public AjaxResult batchGenerateStats(@RequestParam("beginDate") String beginDate, 
                                        @RequestParam("endDate") String endDate)
    {
        try {
            int result = certificateIssuanceStatsService.batchGenerateStatsData(beginDate, endDate);
            return success("批量生成成功，共生成 " + result + " 条统计数据");
        } catch (Exception e) {
            return error("批量生成失败：" + e.getMessage());
        }
    }

    /**
     * 刷新统计数据
     */
    @PreAuthorize("@ss.hasPermi('nongjt:certificateIssuanceStats:refresh')")
    @Log(title = "刷新统计数据", businessType = BusinessType.UPDATE)
    @PostMapping("/refresh/{statsDate}")
    public AjaxResult refreshStats(@PathVariable("statsDate") String statsDate)
    {
        try {
            int result = certificateIssuanceStatsService.refreshStatsData(statsDate);
            return success("刷新成功，共更新 " + result + " 条统计数据");
        } catch (Exception e) {
            return error("刷新失败：" + e.getMessage());
        }
    }
}
